package in.co.codedoc.time;

import java.util.Calendar;

public class DayTime
	implements Comparable<DayTime> 
{
	public DayTime()
	{
		this.day = new Day();
		this.time = new Time();
	}
	
	public DayTime(Day day,Time time)
	{
		this.day = day;
		this.time = time;
	}
	
	public DayTime(Calendar daytime)
	{
		this.day = new Day(daytime);
		this.time = new Time(daytime);
	}

	public DayTime(long offSetInMillis)
	{
		this(null,offSetInMillis);
	}

	public DayTime(Calendar daytime,long offSetInMillis)
	{
		this(OffSetCalendar(daytime,offSetInMillis));
	}
	
	private static Calendar OffSetCalendar(Calendar daytime, long offSetInMillis)
	{
		if(daytime == null)
		{
			daytime = Calendar.getInstance();
		}
		
		daytime.setTimeInMillis(daytime.getTimeInMillis() + offSetInMillis);
		
		return daytime;
	}

	public DayTime(int dayNumber,int millis)
	{
		this.day = new Day(dayNumber);
		this.time = new Time(millis);
	}

	public Calendar GetCalendar()
	{
		Calendar cal = day.GetCalendar();
		
		cal.add(Calendar.MILLISECOND,time.GetMillisNumber());
		
		return cal;
	}
	
	public Day GetDay()
	{
		return day;
	}
	
	public Time GetTime()
	{
		return time;
	}

	public int DaysSince(DayTime dt)
	{
		return day.DaysSince(dt.day);			
	}
	
	@Override
	public int compareTo(DayTime dt)
	{
		int dc = day.compareTo(dt.day);
		
		return (dc == 0) ? time.compareTo(dt.time) : dc;			
	}
	
	@Override
	public boolean equals(Object t)
	{
		if(t == null || !(t instanceof DayTime))
		{
			return false;
		}
		
		DayTime that = (DayTime)t;
		return day.equals(that.day) && time.equals(that.time);
	}
	
	@Override
	public String toString()
	{
		return day.toString() + " " + time.toString();
	}
	
	@Override
	public int hashCode()
	{
		return day.hashCode() ^ time.hashCode();
	}

	private Day day;
	private Time time;
	
}
